package com.insightg3.android;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class GetTaobaoResult {
    	protected static String testUrl = "http://gw.api.taobao.com/router/rest";
    	protected static String appkey = "12201364";
    	protected static String secret = "89ec79f08a30ab437e0c8e8fc05d8349";
    	
    	public static String testUserGet(String q, long id, long startPrice, long endPrice, String orderBy){
        	 TreeMap<String, String> apiparamsMap = new TreeMap<String, String>();
        	 apiparamsMap.put("format", "json");
        	 apiparamsMap.put("method", "taobao.items.get");
        	 apiparamsMap.put("sign_method","md5");
        	 apiparamsMap.put("app_key",appkey);
        	 apiparamsMap.put("page_size", "10");
        	 apiparamsMap.put("page_no", String.valueOf(id));
        	 apiparamsMap.put("v", "2.0");
        	 String timestamp =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        	 apiparamsMap.put("timestamp",timestamp);
        	 apiparamsMap.put("fields","num_iid, title, nick, pic_url, cid, price, type, delist_time, post_fee");//��Ҫ��ȡ���ֶ�
        	 if(!q.equals(""))
        		 apiparamsMap.put("q",q);
        	 if(startPrice > 0 && startPrice < 100000000)
        		 apiparamsMap.put("start_price", String.valueOf(startPrice));
        	 if(endPrice > 0 && endPrice < 100000000)
        		 apiparamsMap.put("end_price", String.valueOf(endPrice));
        	 if(!orderBy.equals(""))
        		 apiparamsMap.put("order_by", orderBy);
        	 // 排序方式。格式为column:asc/desc,column可选值为: price（价格）, delist_time（下架时间）, seller_credit（卖家信用）；默认是按照上架时间倒序。如按价格升序排列表示为：price:asc。新增排序字段：volume（30天成交量） 
        	 String sign = Util.md5Signature(apiparamsMap,secret);
        	 apiparamsMap.put("sign", sign);
        	 StringBuilder param = new StringBuilder();
        	 for (Iterator<Map.Entry<String, String>> it = apiparamsMap.entrySet()
        	 .iterator(); it.hasNext();) {
            	 Map.Entry<String, String> e = it.next();
            	 param.append("&").append(e.getKey()).append("=").append(e.getValue());
        	 }
        	 return param.toString().substring(1);
    	}
}